﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace WebSystem
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //如果已经登录过的，直接转到主页
                if (Session["IsLogin"] != null)
                {
                    if ((DropDownList1.SelectedItem).ToString() == "教师")
                    {
                        Response.Redirect("Teacher Main.aspx");
                    }
                    if ((DropDownList1.SelectedItem).ToString() == "学生")
                    {
                        Response.Redirect("StudentMain.aspx");
                    }
                    if ((DropDownList1.SelectedItem).ToString() == "管理员")
                    {
                        Response.Redirect("MMain.aspx");
                    }
                }
            }
          
          
        }

  

        protected void btnLogin_Click1(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014115121;uid=2014115121;password=19960404";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                if ((DropDownList1.SelectedItem).ToString() == "教师")
                {
                    Session["name"] = this.txtUser.Text;
                    Session["memo"] = "教师";
                    cn.Open();
                    string strSql = "SELECT * FROM UserInfo1 where UserName =N'" + this.txtUser.Text + "'";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    SqlDataReader c = cmd.ExecuteReader();
                    string password = "";
                    if (c.Read())
                    {
                        password = (string)c.GetValue(1);
                        if (password == txtPwd.Text)
                        {
                            Session["IsLogin"] = true;
                            Response.Redirect("Teacher Main.aspx");
                           
                        }
                        else
                        {
                            Response.Write("<script>javascript:alert('密码错误');history.back();</script>");
                        }

                    }
                    else
                    {
                        Response.Write("<script>javascript:alert('没有此用户');history.back();</script>");
                    }
                    c.Close();
                }
                if ((DropDownList1.SelectedItem).ToString() == "学生")
                {
                    Session["name"] = this.txtUser.Text;
                    Session["memo"] = "学生";
                    cn.Open();
                    string strSql = "SELECT * FROM UserInfo2 where UserName =N'" + this.txtUser.Text + "'";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    SqlDataReader c = cmd.ExecuteReader();
                    string password = "";
                    if (c.Read())
                    {
                        password = (string)c.GetValue(1);
                        if (password == txtPwd.Text)
                        {
                            Session["IsLogin"] = true;
                            Response.Redirect("StudentMain.aspx");
                            
                        }
                        else
                        {
                            Response.Write("<script>javascript:alert('密码错误');history.back();</script>");
                        }

                    }

                    else
                    {
                        Response.Write("<script>javascript:alert('没有此用户');history.back();</script>");
                    }
                    c.Close();

                }

                if ((DropDownList1.SelectedItem).ToString() == "管理员")
                {
                    Session["name"] = this.txtUser.Text;
                    Session["memo"] = "管理员";
                    cn.Open();
                    string strSql = "SELECT * FROM UserInfo where UserName =N'" + this.txtUser.Text + "'";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    SqlDataReader c = cmd.ExecuteReader();
                    string password = "";
                    if (c.Read())
                    {
                        password = (string)c.GetValue(1);
                        if (password == txtPwd.Text)
                        {
                            Session["IsLogin"] = true;
                            Response.Redirect("MMain.aspx");
                          
                        }
                        else
                        {
                            Response.Write("<script>javascript:alert('密码错误');history.back();</script>");
                        }

                    }

                    else
                    {
                        Response.Write("<script>javascript:alert('没有此用户');history.back();</script>");
                    }
                    c.Close();

                }

                //if ((DropDownList1.SelectedItem).ToString() == "管理员")
                //{
                //    Session["name"] = this.txtUser.Text;
                //    Session["memo"] = "管理员";
                //    cn.Open();
                //    string strSql = "SELECT * FROM UserInfo where UserName =N'" + this.txtUser.Text + "'";
                //    SqlCommand cmd = new SqlCommand(strSql, cn);
                //    SqlDataReader c = cmd.ExecuteReader();
                //    string password = "";
                //    if (c.Read())
                //    {
                //        password = (string)c.GetValue(1);
                //        if (password == txtPwd.Text)
                //        {

                //            Response.Redirect("MMain.aspx");
                //            Session["IsLogin"] = true;
                            
                //        }
                //        else
                //        {
                //            Response.Write("<script>javascript:alert('密码错误');history.back();</script>");
                //        }

                //    }
                //    else
                //    {
                //        Response.Write("<script>javascript:alert('没有此用户');history.back();</script>");
                //    }
                //    c.Close();
                //}
            }
            catch
            {
                Response.Write("<script>javascript:alert('连接失败');history.back();</script>");
            }
            finally
            {
                cn.Close();
            }
        }

     
        protected void btnExit_Click(object sender, EventArgs e)
        {
            txtUser.Text = "";
            txtPwd.Text = "";
           
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}